package cn.edu.cqu.controllers;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.cqu.domains.Admin;
import cn.edu.cqu.services.AdminCRUD;

@RestController
public class AdminController
{
	@Autowired
	@Qualifier("AdminCRUDHibernate")
	private AdminCRUD adminCRUD;
	
	@RequestMapping(value="/updateAdmin.json")
	public HashMap<String, Object> updateAdmin(HttpServletRequest request)
	{
		HashMap<String, Object> results = new HashMap<String, Object>();
		String aName = request.getParameter("name");
		String aPwd = request.getParameter("pwd");
		Admin admin = new Admin();
		admin.setaName(aName);
		admin.setaPwd(aPwd);
		adminCRUD.edit(admin);
		results.put("status", 0);
		return results;
	}
	
	@RequestMapping(value="/login.json")
	public HashMap<String, Object> login(HttpServletRequest request)
	{
		HashMap<String, Object> results = new HashMap<String, Object>();
		String aID = request.getParameter("aID");
		String aPwd = request.getParameter("pwd");
		if (adminCRUD.getByID(aID).getaPwd().equals(aPwd)) {
			results.put("status", 0);
		} else {
			results.put("status", 1);
		};
		
		return results;
	}
}
